<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>title</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="/css/public.css" media="all">
</head>
<body>
<div class="layuimini-container" id="app">
    <div class="layuimini-main">
        <fieldset class="table-search-fieldset">
            <legend>奖励-{{type==1?'活动返利':'日常奖励'}} </legend>
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" action="" lay-filter="table-search">
                    <div class="layui-form-item">
                        <!--
                        <div class="layui-inline">
                            <label class="layui-form-label">ID</label>
                            <div class="layui-input-inline">
                                <input type="text" name="id" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        -->
                        <input type="hidden" name="type" :value="type">
                        <div class="layui-inline">
                            <button type="submit" class="layui-btn layui-btn-primary"  lay-submit lay-filter="data-search-btn">
                                <i class="layui-icon"></i> 搜 索</button>
                        </div>
                    </div>
                </form>
            </div>
           <!-- <div>
                <p>奖励配置说明：</p>
                <p>1、赠送虚拟充值时，不能填其他货币及道具奖励。商品ID:数量|商品ID:数量   1003:222|1004:20</p>
                <p>2、赠送非虚拟充值时，至少需填入一样道具，不可纯货币。</p>
            </div>-->
        </fieldset>
        <script type="text/html" id="topToolBar">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-sm layui-btn-normal" id="import" auth="rewards:import">导入</button>
                <button class="layui-btn layui-btn-sm layui-btn-danger" lay-event="batchDelete" auth="rewards:delete">
                    <i class="layui-icon layui-icon-delete"></i>批量删除</button>
            </div>
        </script>
        <table class="layui-hide" id="current-table" lay-filter="current-table"></table>
        <script type="text/html" id="currentTableBar">
            <a class="layui-btn layui-btn-xs layui-btn-danger" lay-event="delete" auth="rewards:delete"><i class="layui-icon layui-icon-delete"></i>删除</a>
        </script>
    </div>
</div>
<script src="/lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script src="/js/common.js?v=5" charset="utf-8"></script>
<script>
var type = urlParam("type")
new Vue({
    el: "#app",
    data: {
        services: JSON.parse(localStorage.getItem('services')),
        type:type,
    },
    created() {

    },
    updated() {

    },
    mounted() {
        layui.use(['form', 'table','upload'], function () {
            var $ = layui.jquery,
                form = layui.form,
                upload = layui.upload,
                table = layui.table;

            table.render({
                elem: '#current-table',
                url: "/admin/rewards/paginate",
                toolbar: '#topToolBar',
                where:{type:type},
                defaultToolbar: ['filter'],
                cols: [[
                    {type: "checkbox", width: 50},
                    {field: "sort", title: "序号"},
                    {field: "title", title: "邮件标题"},
                    {field: "content", title: "邮件正文"},
                    {field: "server_id", title: "服务器ID"},
                    {field: "customer_id", title: "网站账号ID"},
                    {field: "yuanbao", title: "金币"},
                    {field: "hxb", title: "金卷"},
                    {field: "yinyuan", title: "银币"},
                    {field: "tongbi", title: "铜币"},
                    {field: "payjinbi", title: "返利金币"},
                    {field: "equips", title: "奖励道具" },
                    {field: "begin_time", title: "开始时间"},
                    {field: "end_time", title: "结束时间"},
                    {field: "status", title: "结束时间",templet:function(row){
                            var values = [{"value":0,"title":"未领取"},{"value":1,"title":"已领取"}];
                            var value = row.status;
                            for(var k in values){
                                if(value === values[k].value ){
                                    return values[k].title
                                }
                            }
                            return row.status;
                        }},
                    {title: '操作', minWidth: 150, toolbar: '#currentTableBar', align: "center"}
                ]],
                limits: [10, 15, 20, 25, 50, 100],
                limit: 15,
                page: true,
                skin: 'line'
            });
            form.on('submit(data-search-btn)', function (data) {
                var searchData = data.field;
                //执行搜索重载
                table.reload('current-table', {
                    page: { curr : 1 }
                    , where:searchData
                }, 'data');
                return false;
            });

            upload.render({
                elem: '#import',
                data:{type:type,},
                exts:"xlsx",
                acceptMime:"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
                trigger: 'click', //绑定点击事件
                url:"/admin/rewards/import",
                done:function(res){
                    if (res.code !==0 ){
                        layer.msg(res.msg,{icon:2})
                    } else {
                        layer.msg('导入成功',{icon:1})
                        location.reload();
                    }
                }
            });

            table.on('toolbar(current-table)', function (obj) {
                if (obj.event === 'insert') {
                    layer.open({
                        title: "奖励-添加",
                        type: 2,
                        shade: 0.2,
                        maxmin:true,
                        shadeClose: true,
                        area: ['700px', '500px'],
                        content: 'form.html',
                    });
                } else if(obj.event==='batchDelete'){
                    var checkStatus = table.checkStatus(obj.config.id); //获取选中行状态
                    var data = checkStatus.data;  // 获取选中行数据
                    if (data.length===0) return;
                    var deleteNum = 0;
                    var delPop = layer.confirm('确定要批量删除吗?',{title:"提示",icon:3},function(){
                        for(var k in data){
                            var id = data[k].id;
                            $.post("/admin/rewards/" + id,{_method:'delete'},function (res) {
                                if (res.code === 0){
                                    deleteNum++
                                }
                            });
                        }
                        var interval =setInterval(function () { //判断删除数量
                            if (deleteNum===data.length){ //删除完毕
                                layer.msg('删除成功',{icon:1,time:500},function(){
                                    table.reload('current-table');
                                })
                                layer.close(delPop)
                                table.reload()
                                clearInterval(interval)
                            }
                        },10)
                    })

                } else {
                    console.log(1)
                }
            });

            table.on('tool(current-table)', function (obj) {
                if (obj.event === 'update') {
                    layer.open({
                        title: "奖励-编辑",
                        type: 2,
                        shade: 0.2,
                        maxmin:true,
                        shadeClose: true,
                        area: ['700px', '500px'],
                        content: 'form.html?id='+obj.data.id,
                    });
                    return false;
                } else if (obj.event === 'delete') {
                    var delPop = layer.confirm('确定要删除吗?',{title:"提示",icon:3},function(){
                        $.post("/admin/rewards/"+obj.data.id,{_method:'delete'},function (res) {
                            if (res.code === 0){
                                layer.msg('删除成功',{icon:1,time:500},function(){
                                    table.reload('current-table');
                                })
                                table.reload()
                                layer.close(delPop)
                            } else {
                                layer.msg(res.msg,{icon:2})
                            }
                        });
                    })
                }
            });
        });
    }
})

</script>
</body>
</html>
